package gov.nhso.prov.acc.utils;

import gov.nhso.prov.acc.core.ConfigBundle;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
/**
 * 
 * @author Pisanui
 * 
 * System message
 * type
 * I = Infomation
 * E = Error
 * W = Warning
 * H = Helper
 *
 */
@SuppressWarnings("rawtypes")
public class Message {
	private HttpServletRequest request;
	
	protected Message (HttpServletRequest request) {
		this.request = request;
	}
	
	public void clearMsg() {
		this.request.getSession().removeAttribute("msg");
	}
	
	public boolean haveMsg() {
		boolean m = false;
		if (this.request.getSession().getAttribute("msg") != null) {
			m = true;
		}
		return m;
	}
	
	public void setMsg(String type, String text) {
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("type", type);
		msg.put("text", text);
		msg.put("detail", null);
		this.request.getSession().setAttribute("msg", msg);
	}
	
	public void setMsg(String type, String text, String detail) {
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("type", type);
		msg.put("text", text);
		msg.put("detail", detail);
		this.request.getSession().setAttribute("msg", msg);
	}
	
	public void setMsg(String type, String text, StackTraceElement[] ste) {
		if(ConfigBundle.getValue("SHOW_ERROR_MSG").equals("Y")){
			Map<String, String> msg = new HashMap<String, String>();
			msg.put("type", type);
			msg.put("text", text);
			StringBuffer detail = new StringBuffer();
			for (int i = 0; i < ste.length; i++) {
				detail.append(ste[i].toString() + "<br>");
			}
			msg.put("detail", detail.toString());
			this.request.getSession().setAttribute("msg", msg);
		} else if (this.request.getSession().getAttribute("SHOW_ERROR_MSG") != null && this.request.getSession().getAttribute("SHOW_ERROR_MSG").equals("Y")) {
			Map<String, String> msg = new HashMap<String, String>();
			msg.put("type", type);
			msg.put("text", text);
			StringBuffer detail = new StringBuffer();
			for (int i = 0; i < ste.length; i++) {
				detail.append(ste[i].toString() + "<br>");
			}
			msg.put("detail", detail.toString());
			this.request.getSession().setAttribute("msg", msg);
		} else if (type.equals("W")) {
			Map<String, String> msg = new HashMap<String, String>();
			msg.put("type", type);
			msg.put("text", text);
			StringBuffer detail = new StringBuffer();
			for (int i = 0; i < ste.length; i++) {
				detail.append(ste[i].toString() + "<br>");
			}
			msg.put("detail", detail.toString());
			this.request.getSession().setAttribute("msg", msg);
		}
	}
	
	public void setMsgCode(String msgCode) {
		try {
			Map<String, String> msg = new HashMap<String, String>();
			String type = msgCode.substring(0, 1);
			if (!type.matches("[EWIH]")) {
				type = "W";
			}
			msg.put("type", type);
			msg.put("text", this.getMsg(msgCode));
			msg.put("detail", null);
			this.request.getSession().setAttribute("msg", msg);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	private String getMsg(String msgCode) throws IllegalArgumentException, IllegalAccessException {
		String msgText = "not found code message";
		Class clazz = this.getClass();
		Field[] f = clazz.getDeclaredFields();
		for (int i = 0; i < f.length; i++) {
			Field fld = f[i];
			if (msgCode.equals(fld.getName())) {
				fld.setAccessible(true);
				msgText = (String) fld.get(this);
				fld.setAccessible(false);
				break;
			}
		}
		return msgText;
	}
	
	protected String E001 = "message error E001";
	protected String H001 = "message helper H001";
	protected String I001 = "ไม่พบข้อมูล";
	protected String I002 = "บันทึกข้อมูลสำเร็จ";
	protected String I003 = "ลบข้อมูลสำเร็จ";
	protected String W001 = "ไม่สามารถบันทึกได้";
	protected String W002 = "ค้นหาข้อมูลไม่พบ";
}
